﻿#include "responesutils.h"

#include <QFile>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>


ResponesUtils::ResponesUtils()
{

}

ResponesUtils::~ResponesUtils()
{

}

void ResponesUtils::resultResponse(stefanfrings::HttpResponse &response, QJsonObject data)
{
    response.setStatus(200);
    QJsonObject obj;
    obj.insert("code",0);
    obj.insert("data",data);
    obj.insert("error",QJsonValue());
    obj.insert("message","ok");
    response.write(QJsonDocument(obj).toJson());
}

void ResponesUtils::resultResponse(HttpResponse &response, QJsonArray data)
{
    response.setStatus(200);
    QJsonObject obj;
    obj.insert("code",0);
    obj.insert("data",data);
    obj.insert("error",QJsonValue());
    obj.insert("message","ok");
    response.write(QJsonDocument(obj).toJson());
}

void ResponesUtils::resultResponse(HttpResponse &response, QJsonValue data)
{
    response.setStatus(200);
    QJsonObject obj;
    obj.insert("code",0);
    obj.insert("data",data);
    obj.insert("error",QJsonValue());
    obj.insert("message","ok");
    response.write(QJsonDocument(obj).toJson());
}

void ResponesUtils::binaryResponse(HttpResponse &response, const QByteArray &byteArray, bool compress)
{
    QByteArray disposeData;
    if(compress){
        disposeData = qCompress(byteArray).toBase64();
    }else{
        disposeData = byteArray.toBase64();
    }
    QByteArray resData = QString("{\"code\":0,\"data\":\"%1\",\"error\":\"\",\"message\":\"ok\"}").arg(QString(disposeData)).toLatin1();
    response.write(resData);
}

void ResponesUtils::pagedResultResponse(HttpResponse &response, uint64_t total, QJsonArray list){
    response.setStatus(200);
    QJsonObject obj;
    obj.insert("items",list);
    obj.insert("total",QJsonValue::fromVariant(total));
    resultResponse(response,obj);
}

void ResponesUtils::erroResponse(HttpResponse &response,QJsonValue error){
    QJsonObject obj;
    obj.insert("code",-1);
//    obj.insert("data",QJsonValue());
    obj.insert("error",error);
    obj.insert("message",error);
//    qDebug() << "ResponseError" << QJsonDocument(obj).toJson();
    response.write(QJsonDocument(obj).toJson());
}

//void ResponesUtils::erroResponse(HttpResponse &response, QString message)
//{
//    response.setStatus(404);
//    erroResponse(response,message,message);
//}

void ResponesUtils::forbiddenResponse(HttpResponse &response,QString message){
    response.setStatus(403);
    erroResponse(response,message);
}

void ResponesUtils::unAuthorizedResponse(HttpResponse &response)
{
    response.setStatus(401);
    erroResponse(response,"Unauthorized Exception");
}

void ResponesUtils::downloadFileResponse(HttpResponse &response, const QString &filePath,QString fileName)
{
    // 打开文件并读取内容到响应体中
    QFile file(filePath);
    if (file.open(QIODevice::ReadOnly)) {
        // 设置响应头部信息
        response.setStatus(200, "OK");
        response.setHeader("Content-Type", "application/octet-stream");
        if(fileName.isEmpty()){
            fileName = QFileInfo(filePath).fileName();
        }
        response.setHeader("Content-Disposition", QString("attachment; filename=\"%1\"").arg(fileName).toUtf8());
        response.setHeader("Content-Length", QString::number(file.size()).toLatin1());

        QByteArray fileData = file.read(1024*1024*2);
        while (fileData.size() > 0) {
            // 将文件数据写入HTTP响应
            response.write(fileData); // 第二个参数表示是否立即刷新缓冲区
            fileData = file.read(1024*1024*2);
        }
        file.close();
    } else {
        ResponesUtils::erroResponse(response, "无法打开文件！");
    }
}
